Ext.define('PJ.view.setassessmentpaper.CourseSelectStaffList', {
    extend: 'AM.default.DefaultList',
    
    alias: ['widget.courseSelectStaffList'],
    
    queryUnitId: null,
    searchTreePath: null,
    overflowX: 'auto',
    overflowY: 'hidden',
    columns: [{
        header: '问卷名称',
        dataIndex: 'questionnaireName',
        flex: 1,
        minWidth: 120,
		renderer: function(value, metaData, record){
			metaData.tdAttr = 'data-qtip="' + value + '"';
			return value;
		}
    },{
        header: '问卷说明',
        dataIndex: 'questionnaireExplain',
        flex: 1,
        minWidth: 120,
        renderer: function(value, metaData, record){
            return value;
        }
    },{
        header: '创建时间',
        dataIndex: 'q.createDate',
        width: 100,
        renderer: function(value, metaData, record){
            return value;
        }
    }],
    
    initComponent: function(){
        this.store = Ext.create('PJ.store.setassessmentpaper.CourseSelectStaffList');
        this.loadType = gj_onlineLearning_constant.loadType;//加载方式
        this.callParent(arguments);
    },

    onAddClick: function(button){
    	var me = this;
    	var records = this.getSelectionModel().getSelection();
    	if (records.length > 1) {
            Ext.Msg.show({
                title: '警告',
                msg: '操作时只可选中一条记录！',
                icon: Ext.Msg.WARNING,
                buttons: Ext.Msg.OK
            });
            return;
        }
    	var userIds = new Array();
    	Ext.Array.each(records, function(name, index){
    		userIds.push(name.get("questionnaireId"));
    	});
//    	me.loadMask.show();
    	if(gj_onlineLearning_constant.loadType == "jsonp"){
    		var store = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    			loadType: gj_onlineLearning_constant.loadType,
    			url: me.store.addUrl,
    			extraParams: {
    				surveyObjectIds:me.up('window').classCourseId,
    				questionnaireId:userIds
    			}
    		});
    		store.load(function(rec, operation, success){
//    			me.loadMask.hide();
    			if(success){
    				Orchid.alert('消息', '添加成功！', function(){
    					var store = me.getStore();
    					store.load();
    					me.up("window").grid.getStore().load();
    					me.up('window').close();
    				});
    			}else{
    				Orchid.alert('消息', operation.error);
    			}
    		});
    		/*
            	var store1 = Ext.create('AM.default.jsonp.DefaultAjaxStore',{
    				loadType: gj_onlineLearning_constant.loadType,
    				url: gj_onlineLearning_constant.tokenUrl,
    				extraParams: {}
    			});
    			store1.load(function(rec, operation, success){
    				var token = operation.response;
    				if(success){

    				}else{
    					Orchid.alert('消息', operation.error);
    				}
    			});
    		 */
    	}else if(gj_onlineLearning_constant.loadType == "ajax"){
    		Ext.Msg.confirm({
    			title: '提示',
    			msg: '您确定要添加这些信息吗?',
    			buttons: Ext.Msg.OKCANCEL,
    			fn: function(id){
    				if (id == 'ok') {
//    					me.loadMask.show();
    					Ext.Ajax.request({
    						url: me.store.addUrl,
    						params: {
    		    				surveyObjectIds:me.up('window').classCourseId,
    		    				questionnaireId:userIds
    		    			},
    						success: function(response){
//    							me.loadMask.hide();
    							var data = response.responseText;
    							var json = Ext.decode(data, true);
    							if (json.success) {
//  								Ext.Msg.alert('消息', json.msg, function(){
    								Orchid.alert('消息', '添加成功！', function(){
    									var store = me.getStore();
    									store.load();
    									me.up("window").grid.getStore().load();
    									me.up('window').close();
    								});
    							}else {
    								Ext.Msg.alert('消息', json.msg, function(){
    									var store = me.getStore();
    									store.load();
    									me.up("window").grid.getStore().load();
    									me.up('window').close();
    								});
    							}
    						}
    					});
    				}
    				else {
    					Ext.getCmp("loadingMask").hide();
    					return;
    				}
    			}
    		});
    		/*
            	Ext.Ajax.request({
                    url: gj_onlineLearning_constant.tokenUrl,
                    success: function(response){
                        Ext.getCmp("loadingMask").hide();
                        var data = response.responseText;
                        var token = Ext.decode(data, true);

                    }
                });
    		 */
    	}
    },
    listeners: {
        selectionchange: function(selModel, selected){
            this.down('#' + this.oldId + "Add").setDisabled(selected.length === 0);
        },
        viewready: function(){
        	var me = this;
    		this.addDocked([/*{
                dock: 'top',
                xtype: "searchform",
                columnCount: 1,
                fieldDefaults: {
                    labelAlign: 'right',
                    labelWidth: 100
                },
                items: [{
    				xtype: "textfield",
    				fieldLabel: '问卷名称',
    				name: "searchQuestionnaireName",
    				maxLength: "50",
    				emptyText: "请输入问卷名称"
    			}],
                grid: this,
                onResetClick: function(button){
                    var form = button.up('form');
                    var baseForm = form.getForm();
                    var gridPanel = form.grid;
                    var newParams = {
                    		searchType: "Nonselected",
                    		searchTrainingRoleId: gridPanel.up("window").trainingRoleId,
                    		searchClassId: gridPanel.up("window").classId,
                    		searchDepId: me.queryUnitId
                    };
                    gridPanel.getStore().proxy.extraParams = {};
                    
                    var params = gridPanel.getStore().proxy.extraParams;
                    Ext.apply(params, newParams);
                    
                    form.getForm().reset();
                }
            },*/{
    			xtype: 'toolbar',
    			dock: 'top',
    			displayInfo: true,
    			items: [{
    	        	text: '添加',
    	            iconCls: 'addIco',
    	            itemId: this.oldId + "Add",
    	            scope: this,
    	            disabled: true,
    	            handler: function(button){
    	                button.up("grid").onAddClick(button);
    	            }
    	        }, "->", "->",this.countSelectPanel]
    		}]);
    		// 触发权限过滤事件
    		this.fireEvent("permission", this);
    		var store = me.getStore();
            var newParams = {
            		surveyObjectId:this.up('window').classCourseId,
            		searchType:'Nonselected'
            };
            var params = me.getStore().proxy.extraParams;
            Ext.apply(params, newParams);
            store.isLoad = true;
            store.load();
    	},
    	afterrender: function(){
            this.loadMask = new Ext.LoadMask(this, {
                msg: "正在加载中,请稍后...."
            });
        }
    }
});
